﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SyncService
{
    public class Logs
    {
        private static object ctx = new object();

        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="filePrefix">文件前缀</param>
        /// <param name="title">日志标题</param>
        /// <param name="content">日志内容</param>
        private static void WriteOperateLog(string filePrefix, string title, string content, bool IsSuccess)
        {
            StreamWriter sw = null;
            DateTime date = DateTime.Now;
            string foder = date.ToString("yyyy-MM-dd");//date.Year.ToString() + "/" + date.Month.ToString()+"/"+date.Day.ToString();
            string tag = "error";
            string FileName = string.Empty;
            try
            {
                string LogsRootPath = ConfigurationManager.AppSettings["LogsRootPath"].ToString();

                if (IsSuccess)
                    tag = "success";
                //string logName = tag + filePrefix + date.ToString("dd") + ".log";
                string logName = tag + filePrefix + ".log";
                FileName = LogsRootPath + "/" + foder + "/" + logName;

                #region 检测日志目录是否存在
                if (!Directory.Exists(LogsRootPath + "/" + foder + ""))
                {
                    Directory.CreateDirectory(LogsRootPath + "/" + foder + "");
                }
                lock (ctx)
                {
                    if (!File.Exists(FileName))
                        sw = File.CreateText(FileName);
                    else
                        sw = File.AppendText(FileName);
                }
                #endregion

                sw.WriteLine("Title：  " + title);
                sw.WriteLine("Content：" + content + "\r");
                sw.WriteLine("Time：   " + date.ToString());
                sw.WriteLine("---------------------------------------------------\r");
                sw.Flush();
                sw.Close();
            }
            catch(Exception ex)
            {

            }
            finally
            {
                if (sw != null)
                    sw.Close();
            }

        }

        /// <summary>
        /// 添加失败日志
        /// </summary>
        /// <param name="filePrefix"></param>
        /// <param name="title"></param>
        /// <param name="content"></param>
        public static void WriteOperateErrorLog(string filePrefix, string title, string content)
        {
            WriteOperateLog(filePrefix, title, content, false);
        }

        /// <summary>
        /// 添加成功日志
        /// </summary>
        /// <param name="filePrefix"></param>
        /// <param name="title"></param>
        /// <param name="content"></param>
        public static void WriteOperateSuccessLog(string filePrefix, string title, string content)
        {
            WriteOperateLog(filePrefix, title, content, true);
        }
    }

}
